cmake_minimum_required(VERSION 3.15)
project(WinLauncher)

include(CheckSymbolExists)

if (NOT WIN32)
  message(FATAL_ERROR "Windows only.")
endif ()

set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} /i${CMAKE_CURRENT_SOURCE_DIR}")

if (DEFINED ENV{BUILD_NUMBER})
  set(BUILD_NUMBER $ENV{BUILD_NUMBER})
else ()
  set(BUILD_NUMBER 9999)
endif ()

string(TIMESTAMP YEAR "%Y")

# set(JAVA_HOME $ENV{JDK_11_x64})
set(JAVA_HOME $ENV{JAVA_HOME}) # modified
if (NOT JAVA_HOME)
  message(FATAL_ERROR "JDK_11_x64 variable not defined.")
endif ()

set(OUTPUT_BASE_NAME jstart)

check_symbol_exists("_M_AMD64" "" TARGET_X64)
if (TARGET_X64)
  set(BITS_SUFFIX "64")
  set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} /d X64")
else ()
  set(BITS_SUFFIX "")
endif ()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/WinLauncher.rc ${CMAKE_CURRENT_BINARY_DIR}/WinLauncher.rc @ONLY)

add_executable(${OUTPUT_BASE_NAME}
  WIN32
  WinLauncher.cpp
  WinLauncher.manifest
  ${CMAKE_CURRENT_BINARY_DIR}/WinLauncher.rc
)

set_target_properties(${OUTPUT_BASE_NAME} PROPERTIES
  OUTPUT_NAME "${OUTPUT_BASE_NAME}${BITS_SUFFIX}"
  CXX_STANDARD 14
  MSVC_RUNTIME_LIBRARY "MultiThreaded"
)

target_compile_definitions(${OUTPUT_BASE_NAME}  PRIVATE UNICODE _UNICODE)
target_include_directories(${OUTPUT_BASE_NAME}  PRIVATE ${JAVA_HOME}/include ${JAVA_HOME}/include/win32)
# target_compile_options(WinLauncher PRIVATE /W3 /Zi /O2 /Oi /Gy /GL) # modified
target_compile_options(${OUTPUT_BASE_NAME}  PRIVATE /W3 /Zi /Oi /Gy /GL) # modified
target_link_options(${OUTPUT_BASE_NAME}  PRIVATE /INCREMENTAL:NO /DEBUG /LTCG /OPT:REF /OPT:ICF)
